package com.ajocer.springbootinit.config;

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ajocer.springbootinit.langchain.XingfengChatModel;
import com.ajocer.springbootinit.langchain.XingfengEmbeddingModel;
import com.ajocer.springbootinit.manager.SparkClient;
import com.ajocer.springbootinit.manager.constant.SparkApiVersion;

/**
 * LangChain4j配置类
 */
@Configuration
public class LangChain4jConfig {

    @Value("${xingfeng.api-host}")
    private String apiHost;

    @Value("${xingfeng.app-id}")
    private String appId;

    @Value("${xingfeng.api-key}")
    private String apiKey;

    @Value("${xingfeng.api-secret}")
    private String apiSecret;
    
    @Value("${xingfeng.model:Spark X1-32K}")
    private String model;
    
    @Value("${xingfeng.embedding-api-host:https://emb-cn-huabei-1.xf-yun.com/}")
    private String embeddingApiHost;

    /**
     * 配置SparkClient
     */
    @Bean
    public SparkClient sparkClient() {
        SparkClient client = new SparkClient();
        client.appid = appId;
        client.apiKey = apiKey;
        client.apiSecret = apiSecret;
        return client;
    }

    /**
     * 配置讯飞星火聊天模型
     */
    @Bean
    public ChatLanguageModel chatLanguageModel(SparkClient sparkClient) {
        // 使用X1模型的默认参数
        double temperature = 0.7;
        int maxTokens = 8192;
        double presencePenalty = 1.0;
        double frequencyPenalty = 0.02;
        
        // 根据模型名称选择不同的配置
        if (model.contains("X1")) {
            return new XingfengChatModel(sparkClient, temperature, maxTokens, presencePenalty, frequencyPenalty);
        } else {
            // 兼容旧版本模型
            return new XingfengChatModel(sparkClient, temperature, 4096);
        }
    }

    /**
     * 配置讯飞星火嵌入模型
     */
    @Bean
    public EmbeddingModel embeddingModel() {
        // 使用专用的Embedding API
        return new XingfengEmbeddingModel(embeddingApiHost, appId, apiKey, apiSecret, "query");
    }
    
    /**
     * 配置知识库文档嵌入模型
     */
    @Bean
    public EmbeddingModel documentEmbeddingModel() {
        // 使用专用的Embedding API，设置为para类型（知识原文向量化）
        return new XingfengEmbeddingModel(embeddingApiHost, appId, apiKey, apiSecret, "para");
    }
} 